import { defineStore } from 'pinia'
import { getLocalState, setLocalState } from './helper'
import router from '@/router'

const useChatStore = defineStore('chat-store', {
  state: (): Chat.ChatState => getLocalState(),

  getters: {

    getEnabledNetwork(state) {
      return state.network == true;
    },
    getChatHistoryByCurrentActive(state: Chat.ChatState) {
      const index = state.history.findIndex(item => item.uuid == state.active)
      if (index !== -1)
        return state.history[index]
      return null
    },

    getChatByUuid(state: Chat.ChatState) {
      return (uuid?: number) => {
        if (uuid)
          return state.chat.find(item => item.uuid == uuid)?.data ?? []
        return state.chat.find(item => item.uuid == state.active)?.data ?? []
      }
    },
  },

  actions: {

    addHistory(history: Chat.History, chatData: Chat.Chat[] = []) {
      this.history.push(history)
      this.chat.push({ uuid: history.uuid, data: chatData })
      this.active = history.uuid
      this.recordState()
    },

    updateHistory(uuid: number, edit: Partial<Chat.History>) {
      const index = this.history.findIndex(item => item.uuid == uuid)
      if (index !== -1) {
        this.history[index] = { ...this.history[index], ...edit }
        this.recordState()
      }
    },

    async deleteHistory(uuid: number) {
      const index = this.history.findIndex(item => item.uuid == uuid)
      if(index != -1){
        this.history.splice(index, 1)
        this.recordState()
      }

      const chatIndex = this.chat.findIndex(item => item.uuid == uuid)
      if (chatIndex !== -1) {
        this.chat.splice(chatIndex, 1)
        this.recordState()
      }

      if (this.history.length == 0) {
        this.active = null
        this.recordState()
        return
      }

      if (index > 0 && index <= this.history.length) {
        const uuid = this.history[index - 1].uuid
        this.active = uuid
        this.recordState()
      }

      if (index == 0) {
        if (this.history.length > 0) {
          const uuid = this.history[0].uuid
          this.active = uuid
          this.recordState()
        }
      }

      if (index > this.history.length) {
        const uuid = this.history[this.history.length - 1].uuid
        this.active = uuid
        this.recordState()
        return;
      }
    },

    async setActive(uuid: number) {
      this.active = uuid
    },

    getChatByUuidAndIndex(uuid: number, index: number) {
      if (!uuid || uuid == 0) {
        if (this.chat.length)
          return this.chat[0].data[index]
        return null
      }
      const chatIndex = this.chat.findIndex(item => item.uuid == uuid)
      if (chatIndex !== -1)
        return this.chat[chatIndex].data[index]
      return null
    },

    addChatByUuid(uuid: number, chat: Chat.Chat) {
      const index = this.chat.findIndex(item => item.uuid == uuid)
      if (index !== -1) {
        this.chat[index].data.push(chat)
        const h_index = this.history.findIndex(item => item.uuid == uuid)
        if(h_index !=-1){
          if (this.history[h_index].title == 'New Chat'){
            let value = chat.text
            if(value && value.length > 7)
              value = value.substring(0,7) + '...'
            this.history[h_index].title = value
          }
        }
        this.recordState()
      }
    },

    updateChatByUuid(uuid: number, index: number, chat: Chat.Chat) {
      const chatIndex = this.chat.findIndex(item => item.uuid == uuid)
      if (chatIndex !== -1) {
        this.chat[chatIndex].data[index] = chat
        this.recordState()
      }
    },
    updateLastChatByUuid(uuid: number, chat: Chat.Chat) {
      const chatIndex = this.chat.findIndex(item => item.uuid == uuid)
      if (chatIndex !== -1) {
        this.chat[chatIndex].data[this.chat[chatIndex].data.length - 1] = chat
        this.recordState()
      }
    },

    deleteChatByUuid(uuid: number, index: number) {
      const chatIndex = this.chat.findIndex(item => item.uuid == uuid)
      if (chatIndex !== -1) {
        this.chat[chatIndex].data.splice(index, 1)
        this.recordState()
      }
    },

    clearChatByUuid(uuid: number) {
      if (!uuid || uuid == 0) {
        if (this.chat.length) {
          this.chat[0].data = []
          this.history[0].title = 'New Chat'
          this.recordState()
        }
        return
      }

      const index = this.chat.findIndex(item => item.uuid == uuid)
      if (index !== -1) {
        this.chat[index].data = []
        this.history[index].title = 'New Chat'
        this.recordState()
      }
    },

    recordState() {
      setLocalState(this.$state)
    },
  },
})


export default useChatStore
